﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Event_SG_EWDT
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string redirectUrl = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["groupid"] != null)
            {
                string q = Request.QueryString["groupid"].ToString(); //Invited users...

                redirectUrl = "~/ViewGroup.aspx?groupid=" + q;
            }
            else if (Request.QueryString["eventid"] != null)
            {
                string q = Request.QueryString["eventid"].ToString(); //Invited users...

                redirectUrl = "~/ViewEvent.aspx?eventid=" + q;

            }
        }

        protected void btnRegCreate_Click(object sender, EventArgs e)
        {
            if (tbxRegSignUpEmail.Text == "" || tbxRegSignUpPassword.Text == "" || tbxRegSignUpFullName.Text == "")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! One or more required field was not valid!');", true);
                return;
            }

            bool pass = MemberControls.Registration(tbxRegSignUpEmail.Text, tbxRegSignUpPassword.Text, tbxRegSignUpFullName.Text, "", tbxRegSignUpEmail.Text, 1);

            if (pass)
            {
                //if (Session["PreviousPageUrl"] != null)
                //{
                //    string url = (string)Session["PreviousPageUrl"];

                //    Response.Redirect(url + "&firsttime=1");
                //}
                //else
                //{
                //Session["flag"] = "login";
                //Response.Redirect("~/index.aspx");
                //}

                MemberControls.VerifyAccount(tbxRegSignUpEmail.Text, tbxRegSignUpPassword.Text); //Create login session

                Session["Member"] = MemberControls.VerifyAccount(tbxRegSignUpEmail.Text, tbxRegSignUpPassword.Text);

                if (redirectUrl == "")
                {
                    Session["flag"] = "firsttime";
                    //Response.Redirect("~/index.aspx");

                    string url = (string)Session["PreviousPageUrl"];


                    if (url != null)
                    {
                        Response.Redirect(url);
                    }
                    else
                    {
                        Response.Redirect("~/index.aspx");
                    }
                }
                else
                {
                    Session["flag"] = "firsttime";
                    Response.Redirect(redirectUrl);
                }

            }
            else
            {
                //error
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! The email has been taken!');", true);
            }
        }

        protected void btnRegSignIn_Click(object sender, EventArgs e)
        {
            Member m = MemberControls.VerifyAccount(tbxRegSignInEmail.Text, tbxRegSignInPassword.Text);
            if (m!= null)
            {
                Session["Member"] = m.MembershipNo;
                // Response.Redirect("~/index.aspx");

                //if (Session["PreviousPageUrl"] != null)
                //{
                //    string url = (string)Session["PreviousPageUrl"];

                //    Session["flag"] = "login";
                //    Response.Redirect(url);
                //}
                //else
                //{
                //    Session["flag"] = "login";
                //    Response.Redirect("~/index.aspx");
                //}

                if (redirectUrl == "")
                {
                    Session["flag"] = "login";
                    //Response.Redirect("~/index.aspx");

                    string url = (string)Session["PreviousPageUrl"];

                    if (url != null)
                    {
                        Response.Redirect(url);
                    }
                    else
                    {
                        Response.Redirect("~/index.aspx");
                    }
                }
                else
                {
                    Session["flag"] = "login";
                    Response.Redirect(redirectUrl);
                }
            }
            else
            {
                //error...

                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! Wrong login information was provided!');", true);
            }
        }





    }
}